﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SysManage_RolesM_BWebRolesGroup : INTPage
{
    SqlHelp help = new SqlHelp();
    CommonMethod comHelp = new CommonMethod();

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        if (!IsPostBack)
        {
            // 页面权限
            comHelp.GetRight(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), "webRolesGroup" },
                GrvRolesGroup, new string[] { "cmdAdd", "cmdEdit" }, new int[] { GrvRolesGroup.Columns.Count - 2, GrvRolesGroup.Columns.Count - 1 },
                CommonModel.loginOut2);
            // 数据绑定
            QueryBind(0);
        }
    }        

    #region 换页
    protected void GrvRolesGroup_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GrvRolesGroup.PageIndex = e.NewPageIndex;
            QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
        }
        catch
        { }
    }
    #endregion
    #region 查询
    protected void cmdQuery_Click(object sender, EventArgs e)
    {
        QueryBind(1);
        HidQueryFlag.Value = "1";
    }
    //
    protected void QueryBind(int n)
    {
        string StrSql = " select  WRG.RGCode,WRG.RGName,WRG.RGDesc "
            + "from B_RolesGroup WRG where DeleteFlag<>1 ";

        if (n == 1)
        {
            if (!string.IsNullOrEmpty(txtRGCode.Text.ToString()))
            { StrSql += string.Format(" and WRG.RGCode like '%{0}%'", txtRGCode.Text.ToString()); }
            if (!string.IsNullOrEmpty(txtRGName.Text.ToString()))
            { StrSql += string.Format(" and WRG.RGName like '%{0}%'", txtRGName.Text.ToString()); }
            if (!string.IsNullOrEmpty(txtRGDesc.Text.ToString()))
            { StrSql += string.Format(" and WRG.RGDesc like '%{0}%'", txtRGDesc.Text.ToString()); }
        }
        StrSql += " order by WRG.RGCode DESC ";
        DataTable Dt = help.GetDataTable(StrSql);
        GrvRolesGroup.DataSource = Dt;
        GrvRolesGroup.DataBind();
        // 显示页码信息
        if (Dt.Rows.Count > 0)
        {
            // 设置分页行永远显示
            GrvRolesGroup.BottomPagerRow.Visible = true;
            //赋值Label记录条数
            Label label1 = GrvRolesGroup.BottomPagerRow.FindControl("lblCount") as Label;
            label1.Text = Dt.Rows.Count.ToString();
        }
    }
    #endregion
    #region 添加
    protected void cmdAdd_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        //统计列表中与要添加的类型相同的个数
        int ComN = 0;
        //;
        string StrGvNSql = string.Format("select count(*) from B_RolesGroup where RGCode='{0}'", txtRGCode.Text.ToString());
        ComN = Convert.ToInt16(help.RunSqlReturn(StrGvNSql));
        if (ComN < 1)
        {
            //获取当前用户名称
            string UserName = Session["UserCode"].ToString();
            //当前时间
            string StrDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            string StrInsert = string.Format("Insert Into B_RolesGroup(RGCode,RGName,RGDesc,DeleteFlag,AddAuthor,"
                + "AddDate,UpdateAuthor,UpdateDate) Values('{0}','{1}','{2}',{3},'{4}','{5}','{6}','{7}') ",
                txtRGCode.Text.ToString(), txtRGName.Text.ToString(), txtRGDesc.Text.ToString(), "0", UserName, StrDate, UserName, StrDate);
            //添加结果标志
            int result = help.RunSqlInt(StrInsert);
            if (result > 0)
            {
                CommonMethod.regAlertMessage("操作成功-Success!", this);
                QueryBind(0);
                HidQueryFlag.Value = "0";

                txtRGCode.Focus();
            }
            else
            {
                CommonMethod.regAlertMessage("出现错误-Error!", this);
                txtRGCode.Focus();
            }
        }
        else { CommonMethod.regAlertMessage(txtRGCode.Text.ToString() + "  已经存在！", this); }
    }
    #endregion
    #region 编辑
    protected void cmdEdit_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        string StrUpdate = string.Format(" Update B_RolesGroup set RGName='{0}',RGDesc='{1}' where RGCode='{2}' ", 
            txtRGName.Text.ToString(), txtRGDesc.Text.ToString(), txtRGCode.Text.ToString());
        if (help.RunSqlInt(StrUpdate) > 0)
        {
            CommonMethod.regAlertMessage("更新成功！", this);
            QueryBind(0);
            HidQueryFlag.Value = "0";

            cmdAdd.Enabled = true;
            cmdEdit.Enabled = false;
            txtRGCode.Enabled = true;
            txtRGCode.Text = "";
            txtRGDesc.Text = "";
            txtRGName.Text = "";
        }
    }
    #endregion

    #region GridView 行事件
    //Gridview 行事件
    protected void GrvRolesGroup_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //鼠标经过事件
        if (e.Row.RowIndex != -1)
        {
            //设置悬浮鼠标指针形状为"小手"
            e.Row.Attributes["style"] = "Cursor:hand";
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "lastColor=this.style.backgroundColor;this.style.backgroundColor='#CCCC99';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=lastColor;");
                if ("zh-cn".Equals(Session["Language"].ToString()))
                {
                    ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "return confirm('确实要删除？');");
                }
                else
                {
                    ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "return confirm('OK to delete?');");
                }
            }
        }
    }
    //行删除
    protected void GrvRolesGroup_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);

        //获取并判断此项在页面与页面组中是否存在
        string RGCode = GrvRolesGroup.Rows[e.RowIndex].Cells[1].Text.Trim().ToString();
        int ComN = Convert.ToInt16(help.RunSqlReturn(string.Format("select count(*) from B_RolesGroupAndColumn where RGCode='{0}'", RGCode)));
        if (ComN < 1)
        {
            string StrDelete = " Delete from B_RolesGroup where RGCode='" + RGCode + "'";
            if (help.RunSqlInt(StrDelete) > 0)
            {
                CommonMethod.regAlertMessage("删除成功！", this);
                QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
            }
            else
            {
                CommonMethod.regAlertMessage("更新失败！", this);
            }
        }
        else
        {
            CommonMethod.regAlertMessage(RGCode + "  此页面角色组存在于页面与组，现在不可以删除，要删除请先到页面角色组管理删除此页面角色组！", this);
        }
    }
    //行编辑
    protected void GrvRolesGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtRGCode.Text = GrvRolesGroup.Rows[GrvRolesGroup.SelectedIndex].Cells[1].Text.Trim().ToString();
        txtRGCode.Enabled = false;
        txtRGName.Text = GrvRolesGroup.Rows[GrvRolesGroup.SelectedIndex].Cells[2].Text.Trim().ToString();
        txtRGDesc.Text = GrvRolesGroup.Rows[GrvRolesGroup.SelectedIndex].Cells[3].Text.Trim().ToString();

        cmdAdd.Enabled = false;
        cmdEdit.Enabled = true;
    }
    #endregion
    
    //重置
    protected void cmdClear_ServerClick(object sender, EventArgs e)
    {
        cmdAdd.Enabled = true;
        cmdEdit.Enabled = false;
        txtRGCode.Enabled = true;
        txtRGCode.Text = "";
        txtRGDesc.Text = "";
        txtRGName.Text = "";
    }
    // 跳页
    protected void GrvRolesGroup_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "NewPage")
            {
                TextBox tb = GrvRolesGroup.BottomPagerRow.FindControl("txtNewPageIndex") as TextBox;
                int pageindex = Convert.ToInt32(tb.Text);
                GrvRolesGroup.PageIndex = pageindex - 1;
                QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
            }
        }
        catch
        {
            CommonMethod.regAlertMessage("输入不合法-Error format!", this);
        }
    }
}
